package com.hs.telepass.utils;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.wifi.WifiManager;
import android.telephony.TelephonyManager;

public class NetworkStatusUtil {


	/**
     * 判断当前网络是否可用
     * */
	public static boolean isNetworkAvailable(Context context) {
		ConnectivityManager connectivityManager = (ConnectivityManager) context
				.getSystemService(Context.CONNECTIVITY_SERVICE);
        if(null != connectivityManager){
             NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
            if(null != networkInfo && networkInfo.isAvailable()){
                return true;
            }
        }

        return false;
	}

    /**
     * 检查Wifi是否已经启动
     * */
    private static boolean isWifiEnable(Context context) {
        WifiManager manager = (WifiManager) context
                .getSystemService(Context.WIFI_SERVICE);

        return manager.isWifiEnabled();
    }

    /**
     * 检查当前网络是否使用wifi连接
     * */
    private static boolean isWifiConnectedOrConnecting(Context context) {

        ConnectivityManager connectivityManager = (ConnectivityManager) context
                .getSystemService(Context.CONNECTIVITY_SERVICE);
        return connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI).isConnectedOrConnecting();
    }

    /**
     * 检查当前网络是否使用手机网络连接
     * */
    public static boolean isMobileNetWorkConnectedOrConnecting(Context context) {

        ConnectivityManager connectivityManager = (ConnectivityManager) context
                .getSystemService(Context.CONNECTIVITY_SERVICE);

        return connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).isConnectedOrConnecting();
    }

    /**
     * 是否是2G网络
     */
    public static boolean is2GNetWorkType(Context context) {
        return NETWORK_CLASS_2_G == getNetworkClass(context);
    }

    /**
     * 是否是3G网络
     */
    public static boolean is3GNetWorkType(Context context) {
        return NETWORK_CLASS_3_G == getNetworkClass(context);
    }

    /**
     * 是否是4G网络
     */
    public static boolean is4GNetWorkType(Context context) {
        return NETWORK_CLASS_4_G == getNetworkClass(context);
    }

    /** Unknown network class.  */
    public static final int NETWORK_CLASS_UNKNOWN = 0;
    /** Class of broadly defined "2G" networks.  */
    public static final int NETWORK_CLASS_2_G = 1;
    /** Class of broadly defined "3G" networks.  */
    public static final int NETWORK_CLASS_3_G = 2;
    /** Class of broadly defined "4G" networks.*/
    public static final int NETWORK_CLASS_4_G = 3;

    /**
     * Return general class of network type, such as "3G" or "4G".
     */
    public static int getNetworkClass(Context context) {
        TelephonyManager manager = (TelephonyManager) context
                .getSystemService(Context.TELEPHONY_SERVICE);
        int networkType = manager.getNetworkType();

        switch (networkType) {
            case TelephonyManager.NETWORK_TYPE_GPRS:
            case TelephonyManager.NETWORK_TYPE_EDGE:
            case TelephonyManager.NETWORK_TYPE_CDMA:
            case TelephonyManager.NETWORK_TYPE_1xRTT:
            case TelephonyManager.NETWORK_TYPE_IDEN:
                return NETWORK_CLASS_2_G;
            case TelephonyManager.NETWORK_TYPE_UMTS:
            case TelephonyManager.NETWORK_TYPE_EVDO_0:
            case TelephonyManager.NETWORK_TYPE_EVDO_A:
            case TelephonyManager.NETWORK_TYPE_HSDPA:
            case TelephonyManager.NETWORK_TYPE_HSUPA:
            case TelephonyManager.NETWORK_TYPE_HSPA:
            case TelephonyManager.NETWORK_TYPE_EVDO_B:
            case TelephonyManager.NETWORK_TYPE_EHRPD:
            case TelephonyManager.NETWORK_TYPE_HSPAP:
                return NETWORK_CLASS_3_G;
            case TelephonyManager.NETWORK_TYPE_LTE:
                return NETWORK_CLASS_4_G;
            default:
                return NETWORK_CLASS_UNKNOWN;
        }
    }

}
